package com.paishen.peiniwan.module.square;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RadioButton;

import com.paishen.peiniwan.R;
import com.paishen.peiniwan.frame.http.PnwHttpManager;

/**
 * Created by peng on 2015/12/4.
 */
public class SquareFilterDialog extends Dialog {


    public SquareFilterDialog(Context context, int themeResId) {
        super(context, themeResId);
    }


    public interface OnItemSelectedListener {
        void onItemSelected(Object[] items);
    }

    private Params params;

    public void setParams(Params params) {
        this.params = params;
    }

    public static class Params {
        Context context;
        OnItemSelectedListener callback;
        boolean canCancel = false;
        boolean shadow = true;
        boolean defaultGender;
    }

    public static class Builder {
        private final Params params;

        public Builder(Context context) {
            this.params = new Params();
            this.params.context = context;
        }

        public Builder setOnItemSelectedListener(OnItemSelectedListener callback) {
            this.params.callback = callback;
            return this;
        }

        public Builder setDefaultGender(boolean defaultGender) {
            this.params.defaultGender = defaultGender;
            return this;
        }

        public SquareFilterDialog create() {
            final SquareFilterDialog dialog = new SquareFilterDialog(params.context, params.shadow ? R.style.Theme_Light_NoTitle_Dialog : R.style.Theme_Light_NoTitle_NoShadow_Dialog);

            View view = LayoutInflater.from(params.context).inflate(R.layout.dialog_square_filter, null);

            view.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });

            final RadioButton rbGenderAll = (RadioButton) view.findViewById(R.id.rb_gender_all);
            final RadioButton rbGenderMale = (RadioButton) view.findViewById(R.id.rb_gender_male);
            final RadioButton rbGenderFemale = (RadioButton) view.findViewById(R.id.rb_gender_female);

            if (params.defaultGender) {
                rbGenderFemale.setChecked(true);
            } else {
                rbGenderMale.setChecked(true);
            }

            final RadioButton rbOrderActive = (RadioButton) view.findViewById(R.id.rb_order_active);
            final RadioButton rbOrderDist = (RadioButton) view.findViewById(R.id.rb_order_dist);

            view.findViewById(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int gender = PnwHttpManager.Square.SQUARE_FILTER_GENDER_UNLIMITED;
                    if (rbGenderMale.isChecked()) {
                        gender = PnwHttpManager.Square.SQUARE_FILTER_GENDER_MALE;
                    } else if (rbGenderFemale.isChecked()) {
                        gender = PnwHttpManager.Square.SQUARE_FILTER_GENDER_FEMALE;
                    }

                    String order = PnwHttpManager.Square.SQUARE_FILTER_TYPE_RECENT_LOGIN;
                    if (rbOrderDist.isChecked()) {
                        order = PnwHttpManager.Square.SQUARE_FILTER_TYPE_RECENTLY;
                    }
                    Object[] itemValues = new Object[]{gender, order};
                    if (params.callback != null) params.callback.onItemSelected(itemValues);
                }
            });

            dialog.setContentView(view);
            dialog.setCanceledOnTouchOutside(params.canCancel);
            dialog.setCancelable(params.canCancel);
            dialog.setParams(params);
            return dialog;
        }
    }

}
